using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    public class ZY_ContentService : BaseAppService<ZY_ContentDto, ZY_Content, ZY_ContentRepository>
    {
        readonly ExcelHelp _excelHelp;
        public ZY_ContentService(ZY_ContentRepository repository, ExcelHelp excelHelp) : base(repository)
        {
            _excelHelp = excelHelp;
        }



        private LoadPageOffsetModelAuto<ZY_Content, ZY_ContentDto>
            GetLoadPageOffsetMode(PagingModel<ZY_ContentDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking();
            loadPageOffsetModel.QueryWhere = this.CreateQRList()

     .Add("TableType", "=") //票据类型
     .Add("PrimaryId", "=") //表格主键ID
     .Add("TypeCoce", "=") //类别代码
     .Add("ContentCode", "=") //内容代码
     .Add("ContentOptions", "=") //内容选项
     .Add("ContentOther", "=") //内容补充
     .Add("IsHandWritten", "=") //内容是否为手动补充
     .Add("IsChecked", "=");//是否被选择


            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            loadPageOffsetModel.Selector = model => new ZY_Content()
            {
                Id = model.Id,
                TableType = model.TableType,//票据类型
                PrimaryId = model.PrimaryId,//表格主键ID
                TypeCoce = model.TypeCoce,//类别代码
                ContentCode = model.ContentCode,//内容代码
                ContentOptions = model.ContentOptions,//内容选项
                ContentOther = model.ContentOther,//内容补充
                IsHandWritten = model.IsHandWritten,//内容是否为手动补充
                IsChecked = model.IsChecked,//是否被选择
            };
            return loadPageOffsetModel;
        }


        public async Task<IPageModel<ZY_ContentDto>> GetList(PagingModel<ZY_ContentDto>
            pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
        }


        public async Task<string>
            Export(PagingModel<ZY_ContentDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            var DataList = await this.ExportDataAsync(loadPageOffsetModel);
            var excelData = DataList.Select(model => new
            {
                model.TableType,
                TableType_title = "票据类型",
                model.PrimaryId,
                PrimaryId_title = "表格主键ID",
                model.TypeCoce,
                TypeCoce_title = "类别代码",
                model.ContentCode,
                ContentCode_title = "内容代码",
                model.ContentOptions,
                ContentOptions_title = "内容选项",
                model.ContentOther,
                ContentOther_title = "内容补充",
                model.IsHandWritten,
                IsHandWritten_title = "内容是否为手动补充",
                model.IsChecked,
                IsChecked_title = "是否被选择",
            }).ToList();




            return _excelHelp.Create(excelData);
        }

    }

}
